#include "seqstack.h"

stack *init_stack(int size)
{
    // 分配管理结构体
    stack *s = calloc(1, sizeof(stack));
    if(s != NULL)
    {
        s->data = calloc(size, sizeof(int));
        if(s->data == NULL)
        {
            free(s);
            return NULL;
        }

        s->size = size;
        s->top = -1;
    }
    return s;
}

bool is_empty(stack *s)
{
    return s->top == -1;
}

bool is_full(stack *s)
{
    return s->top == s->size-1;
}

bool push(stack *s, int data)
{
    if(is_full(s))
        return false;

    s->data[++s->top] = data;
    return true;
}

bool pop(stack *s, int *data)
{
    if(is_empty(s))
        return false;

    *data = s->data[s->top--];
    return true;
}

void destroy_init(stack *s)
{
    s->top = -1;
}

void destroy(stack *s)
{
    if(s == NULL)
        return;
    
    free(s->data);
    free(s);
}